package com.icemelon.catdog.controller;

import com.icemelon.catdog.pojo.Friend;
import com.icemelon.catdog.pojo.User;
import com.icemelon.catdog.service.IdolService;
import com.icemelon.catdog.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.icemelon.catdog.exception.MyException;
import com.icemelon.catdog.pojo.*;
import com.icemelon.catdog.service.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/")
public class UserController {
    @Autowired
    private IdolService idolService; // 该Service中需要提供一个方法，该方法根据当前用户ID查询其关注（idol）

    private UserService userService;
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private UserMessageService userMessageService;
    @Autowired
    private UserNoticeService userNoticeService;
    @Autowired
    private UserAffairService userAffairService;

    /**
     *业务需求：新关注用户的id及个人信息
     * URL地址d:/user/idol
     *类型：
     *参数id：昵称，性别，联系方式等
     * 返回值：id,username,message
     * {state,message,data}
     */
    @RequestMapping("userId/findIdolById")//查找
    public Result findIdById(Integer... ids) {
        // 调用一个service，根据当前用户ID查询其关注（idol）
        List<Friend> list =idolService.findById(ids);
        // 将查询结果封装到Result中，然后返回这个Result
        return Result.success(list);
    }
    @RequestMapping("UserId/deleteIdolById")//删除
    public Result deleteIdById(Integer... ids){
       int num = idolService.deleteById(ids);
        return Result.success(num);
    }
    @RequestMapping("UserId/insertIdolById")//新增
    public Result insertById(Integer id){
        int user = idolService.insertById(id);
        return Result.success(id);
    }

    //2
    /**
     * 注册用户
     * @param UserName 登录名
     * @param UserPwd  密码
     * @return
     */
    @PostMapping("CreateUser/{UserName}/{UserPwd}")
    public Result CreateUser(@PathVariable String UserName,@PathVariable String UserPwd){
        Integer i=userService.CreateUser(UserName,UserPwd);
       if (i>0){
           return Result.success();
       }
       return Result.fail();
    }

    /**
     * 用户修改个人信息
     * @return userDetails
     */
    @PutMapping("updateUserDetails")
    public Result updateUser(@RequestBody UserDetails UserDetails) throws MyException {
        userDetailsService.updateUserDetails(UserDetails);
        return Result.success();
    }

    /**
     * 查看用户个人信息
     * @param UserId
     * @return userDetails
     */
    @GetMapping("selectUser/{UserId}")
    public Result selectUser(@PathVariable Integer UserId){
        UserDetails userDetails=new UserDetails();
        userDetails=userDetailsService.selectById(UserId);
        return Result.success(userDetails);
    }



    //3
    /**
     * 用户登录
     * @param UserName
     * @param UserPwd
     * @return
     */
    @PostMapping("login/{UserName}/{UserPwd}")
    public Result login(@PathVariable String UserName,@PathVariable String UserPwd) throws MyException {
        User user=userService.selectUser(UserName,UserPwd);
        return Result.success("登录成功",user);
    }

    /**
     * 用户更改密码
     * @param UserName      用户名
     * @param UserOldpwd    旧密码
     * @param UserNewpwd    新密码
     * @return
     */
    @PostMapping("/chagepwd/{UserName}/{UserOldpwd}/{UserNewpwd}")
    public Result chagepwd(@PathVariable String UserName,@PathVariable String UserOldpwd,@PathVariable String UserNewpwd) throws MyException {
        User user = userService.updateUserPwd(UserName, UserOldpwd, UserNewpwd);
        return Result.success("修改成功", user);
    }

    @PostMapping("/message")
    public Result sendMessage(int sender,int receiver,String content){
        userMessageService.sendMessage(sender,receiver,content);
        return Result.success("添加成功");
    }
    @GetMapping("/message")
    public Result getMessage(Integer id){
        Message msg=userMessageService.getMessageById(id);
        return Result.success(msg);
    }
    @DeleteMapping("/message")
    public Result DeleteMessage(Integer... msgIds){
        userMessageService.deleteMessageByUserId(msgIds);
        return Result.success("删除成功");
    }

    @PostMapping("/notice")
    public Result uploadNotice(@RequestBody Notice notice){
        userNoticeService.uploadNotice(notice);
        return Result.success("上传成功");

    }
    @GetMapping("/notice")
    public Result getNotice(Integer... ids){
        List<Notice> list= userNoticeService.getNotice(ids);
        return Result.success(list);
    }
    @DeleteMapping("/notice")
    public Result deleteNotice(Integer... ids){
        userNoticeService.deleteNotice(ids);
        return  Result.success("删除成功");
    }

    @PostMapping("/affair")
    public Result uploadAffair(@RequestBody Affair affair){
        userAffairService.uploadAffair(affair);
        return Result.success("上传成功");
    }
    @GetMapping("/affair")
    public Result getAffair(Integer... ids){
        List<Affair> list= userAffairService.getAffair(ids);
        return Result.success(list);
    }
    @DeleteMapping("/affair")
    public Result deleteAffair(Integer... ids){
        userAffairService.deleteAffair(ids);
        return Result.success("删除成功");
    }

    private UserFanService userFanService;

    @RequestMapping("/fan/delete")
    public Result deleteFriend(Integer... ids){
        userFanService.delectFanUsers(ids);
        return Result.success();
    }

    @RequestMapping("/fan/gets")
    public List<User> getFriend(Integer id){
        return userFanService.getFanUser(id);
    }
}
